﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using Modele;

namespace MoteurAudio
{
    /* Attention cette classe fait appel à un thread C++ ce qui fait qu'elle peut fonctionnée 
     * Sans que cela soit facilement monitorable par Visual Studio
     */
    public   class Lecteur: ILecteur
    {
        public int  numDsp = 1;// identifiant du canal
        public  void Lire(Note n) { 
        
         
            // Variable de controle d'erreur
            FMOD.RESULT result = FMOD.RESULT.OK;

            // on sauvegarde l'octave
            int octave = n.octave;

            //Libération des cannaux non utilisés
            TrouverCanal();

            /* On modélise la hauteur du son à lire selon la note */
            switch (n.note)
            {

                case Modele.Note.NoteEn.A:

                    if (n.alteration == Note.Alteration.No)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/la.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Diese)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/lad.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Bemol)
                    {

                    }
                    break;


                case Modele.Note.NoteEn.B:


                    if (n.alteration == Note.Alteration.No)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/si.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Diese)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/do.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);
                        octave++; //on monte d'un octave car Si# fait passer à la gamme suivante
                    }

                    if (n.alteration == Note.Alteration.Bemol)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/lad.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }
                    break;

                case Modele.Note.NoteEn.C:

                    if (n.alteration == Note.Alteration.No)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/do.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Diese)
                    {

                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/dod.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Bemol)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/si.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);
                        octave--; //on monte d'un octave car Si# fait passer à la gamme suivante
                    }
                    break;

                case Modele.Note.NoteEn.D:

                    if (n.alteration == Note.Alteration.No)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/re.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Diese)
                    {

                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/red.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Bemol)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/do.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);
                        

                    }
                    break;

                case Modele.Note.NoteEn.E:
                    if (n.alteration == Note.Alteration.No)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/mi.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Diese)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/fa.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);
                        
                    }

                    if (n.alteration == Note.Alteration.Bemol)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/red.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }
                    break;

                case Modele.Note.NoteEn.F:

                    if (n.alteration == Note.Alteration.No)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/fa.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Diese)
                    {

                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/fad.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Bemol)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/mi.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);
                        
                    }
                    break;

                case Modele.Note.NoteEn.G:

                    if (n.alteration == Note.Alteration.No)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/sol.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Diese)
                    {

                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/sold.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);

                    }

                    if (n.alteration == Note.Alteration.Bemol)
                    {
                        result = SystemeAudio.systemAudio.createSound("./Sound/bank1/fad.wav", FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL, ref SystemeAudio.sound[numDsp]);
                        

                    }
                    break;
            }
            SystemeAudio.erreurInfos(result);

    

            //effet
            // SystemeAudio.systemAudio.createDSPByType(FMOD.DSP_TYPE.DISTORTION, ref SystemeAudio.dsp[numDsp]);
            // SystemeAudio.dsp[numDsp].setParameter((int)FMOD.DSP_DISTORTION.LEVEL, 1000.0f);
            //  SystemeAudio.erreurInfos(result);

         
            result = SystemeAudio.systemAudio.playSound(FMOD.CHANNELINDEX.FREE, SystemeAudio.sound[numDsp], true, ref SystemeAudio.channel[numDsp]);
            SystemeAudio.erreurInfos(result);
            SystemeAudio.channel[numDsp].setPaused(false);
    
             float freq=0;
            SystemeAudio.channel[numDsp].getFrequency( ref freq);
            // SystemeAudio.channel[numDsp].setFrequency(freq*2);
            //SystemeAudio.channel[numDsp].setDelay(FMOD.DELAYTYPE.END_MS, 1000, 1000);
           // if(
           // SystemeAudio.channel[numDsp].set
            
            
            
            /* Mise à l'octave */         
            switch (n.octave)
            {
                case 0: SystemeAudio.channel[numDsp].setFrequency(freq / 32); break;
                case 1: SystemeAudio.channel[numDsp].setFrequency(freq / 16); ; break;
                case 2: SystemeAudio.channel[numDsp].setFrequency(freq / 8); break;
                case 3: SystemeAudio.channel[numDsp].setFrequency(freq / 4); break;
                case 4: SystemeAudio.channel[numDsp].setFrequency(freq / 2); break;
                case 5: ; break;
                case 6: SystemeAudio.channel[numDsp].setFrequency(freq * 2);  break;
                case 7: SystemeAudio.channel[numDsp].setFrequency(freq * 4);  break;
                case 8: SystemeAudio.channel[numDsp].setFrequency(freq * 8);  break;
                case 9: SystemeAudio.channel[numDsp].setFrequency(freq * 16); break;
                    
            }
            SystemeAudio.channel[numDsp].setLoopCount(0);
            SystemeAudio.channel[numDsp].setVolume(1f);

         }

        public void Lire(string Mp3) { }

        /* Permet l'attribution d'un canal libre */
        public void TrouverCanal() {

            for (int i = 1; i < 80; i++)
            {
                bool isPlaying = false;

                if (SystemeAudio.channel[i] != null)
                {
                    SystemeAudio.channel[i].isPlaying(ref isPlaying);

                    //Si le canal est utilisé
                    if (isPlaying)
                    {
                        // on ne fait rien
                    }
                    else
                    {
                        
                        if (SystemeAudio.channel[i] != null)
                        {
                            SystemeAudio.channel[i].stop();                           

                        }
                        numDsp = i;
                        break; // On a trouvé notre canal, on sort  de la boucle

                    }
                }
                else
                {
                    numDsp = i;
                    break; // On a trouvé notre canal, on sort  de la boucle
                }
            }
        }
    



    }
}
